package com.uenpay.im.ui.old.activity.bankcard;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.pwd.GetPayPwdVercodeActivity;
import com.uenpay.im.callback.IOnBottomDialogListener;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.DialogUtility;
import com.uenpay.im.weight.ZProgressHUD;
import com.uenpay.util.app.base.BaseActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.MessageFormat;

/**
 * @author SL
 * @version 1.0
 * @copyRight http://www.qiyunxin.com
 * @date 2016-6-12 下午3:33:36
 * @function 添加银行卡
 */
public class AddBankCardActivity extends BaseActivity {

    private EditText bank_card_num_et, name_et;
    private ZProgressHUD mProgressHUD;
    private String selectedBankCard = "";
    private TextView bank_card_name_et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_add_bankcard_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
    }

    protected void initView() {
        mProgressHUD = new ZProgressHUD(this);
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.add_bank_card);
        bank_card_num_et = (EditText) findViewById(R.id.bank_card_num_et);
        bank_card_name_et = (TextView) findViewById(R.id.bank_card_name_et);
        name_et = (EditText) findViewById(R.id.name_et);
    }

    protected void initListener() {
        bank_card_name_et.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(AddBankCardActivity.this,
                        BankListActivity.class);
                intent.putExtra("selectedBankCard", selectedBankCard);
                startActivityForResult(intent, 100);
            }
        });
        findViewById(R.id.next_btn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String bankCard = bank_card_num_et.getText().toString();
                String bankName = bank_card_name_et.getText().toString();
                String accountname = name_et.getText().toString();
                if (!TextUtils.isEmpty(bankCard)
                        && !TextUtils.isEmpty(bankName)
                        && !TextUtils.isEmpty(accountname)) {
                    if (bankCard.length() <= 19 && bankCard.length() >= 16) {
                        addBanck(bankName, bankCard, accountname);
                    } else {
                        showCardErrorDailog();
                    }
                }

            }
        });

    }

    /**
     * 添加银行卡
     *
     * @param bankName
     * @param bankCard
     * @param accountname
     */
    private void addBanck(String bankName, String bankCard, String accountname) {
        mProgressHUD.show();
        String url = APIConfiguration.getPayUrl() + "users/" + IMApplication.getInstance().getUcrOpendId() + "/bankcards";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("bank_name", bankName);
            jsonObject.put("card_no", bankCard);
            jsonObject.put("holder_name", accountname);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BankCardHandle.addBankCard(url, jsonObject.toString(), new BankCardHandle.IBankCardListener() {
            @Override
            public void onBankCardResult(int status, String msg) {
                myHandler.sendEmptyMessage(1);
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    IMApplication.showToast(R.string.add_friend_succeed);
                    myHandler.sendEmptyMessage(0);
                } else {
                    if (!TextUtils.isEmpty(msg)) {
                        IMApplication.showToast(msg);
                    }
                }
            }
        });
    }

    private Handler myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0) {
                sendBroadcast(new Intent(BroadcastAction.ACTION_REFRESH_BANK_CARD));
                finish();
            } else if (msg.what == 1) {
                mProgressHUD.dismiss();
            }
            return false;
        }
    });

    /**
     * 银行卡错误
     */
    private void showCardErrorDailog() {
        DialogUtility.showDialog(AddBankCardActivity.this, R.string.crad_error,
                R.string.sure, R.string.cancel, true,
                new IOnBottomDialogListener() {

                    @Override
                    public void onClicked() {

                    }
                });
    }

    private void showDialog(final String bankCard, final String bankName,
                            final String accountname) {
        String send_vercode = MessageFormat.format(
                getResources().getString(R.string.send_vercode),
                IMApplication.getUserMobilePhone());
        DialogUtility.showSingleBtnDialog(AddBankCardActivity.this,
                send_vercode, R.string.sure, false,
                new IOnBottomDialogListener() {

                    @Override
                    public void onClicked() {
                        Intent intent = new Intent(AddBankCardActivity.this,
                                GetPayPwdVercodeActivity.class);
                        intent.putExtra("bankCard", bankCard);
                        intent.putExtra("bankName", bankName);
                        intent.putExtra("accountname", accountname);
                        intent.putExtra("type", 2);
                        startActivity(intent);
                        finish();
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK && data != null) {
            selectedBankCard = data.getStringExtra("bankCard");
            if (!TextUtils.isEmpty(selectedBankCard)) {
                bank_card_name_et.setText(selectedBankCard);
            }
        }
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }
}
